<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="鸡舍名称" prop="houseName">
        <el-input
          v-model="queryParams.houseName"
          placeholder="请输入鸡舍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鸡舍编号" prop="houseCode">
        <el-input
          v-model="queryParams.houseCode"
          placeholder="请输入鸡舍编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="鸡舍状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择鸡舍状态" clearable>
          <el-option
            v-for="dict in dict.type.tb_chicken_houses_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['btlfbreed:houses:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['btlfbreed:houses:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['btlfbreed:houses:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['btlfbreed:houses:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="housesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="houseId" /> -->
      <el-table-column label="鸡舍名称" align="center" prop="houseName" />
      <el-table-column label="鸡舍编号" align="center" prop="houseCode" />
      <el-table-column label="鸡舍容量" align="center" prop="capacity" />
      <el-table-column label="鸡舍状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_chicken_houses_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="当前存栏量" align="center" prop="currentCount" />
      <el-table-column label="上次检查日期" align="center" prop="lastInspectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastInspectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理员" align="center" prop="userName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['btlfbreed:houses:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['btlfbreed:houses:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改鸡舍信息对话框 -->
    <el-dialog :title="title"   fullscreen=true  :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form"  :inline="true" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="鸡舍名称" prop="houseName">
          <el-input v-model="form.houseName" placeholder="请输入鸡舍名称" />
        </el-form-item>
        <el-form-item label="鸡舍编号" prop="houseCode">
          <el-input v-model="form.houseCode" placeholder="请输入鸡舍编号" />
        </el-form-item>
        <el-form-item label="鸡舍容量" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入鸡舍容量" />
        </el-form-item>
        <el-form-item label="鸡舍状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.tb_chicken_houses_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="当前存栏量" prop="currentCount">
          <el-input v-model="form.currentCount" placeholder="请输入当前存栏量" />
        </el-form-item>
        <el-form-item label="上次检查日期" prop="lastInspectionDate">
          <el-date-picker clearable
            v-model="form.lastInspectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次检查日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="管理员" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入管理员" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <div v-show="isShow">
          <el-divider content-position="center">消毒记录信息</el-divider>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbChickenDisinfectionRecords">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbChickenDisinfectionRecords">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="tbChickenDisinfectionRecordsList" :row-class-name="rowTbChickenDisinfectionRecordsIndex" @selection-change="handleTbChickenDisinfectionRecordsSelectionChange" ref="tbChickenDisinfectionRecords">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="消毒日期" prop="disinfectionDate" width="240">
              <template slot-scope="scope">
                <el-date-picker clearable v-model="scope.row.disinfectionDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择消毒日期" />
              </template>
            </el-table-column>
            <el-table-column label="消毒剂名称" prop="disinfectantName" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.disinfectantName" placeholder="请输入消毒剂名称" />
              </template>
            </el-table-column>
            <el-table-column label="消毒剂浓度" prop="disinfectantConcentration" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.disinfectantConcentration" placeholder="请输入消毒剂浓度" />
              </template>
            </el-table-column>
            <el-table-column label="消毒方法" prop="disinfectionMethod" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.disinfectionMethod" placeholder="请输入消毒方法" />
              </template>
            </el-table-column>
            <el-table-column label="操作员" prop="userId" width="150">
              <template slot-scope="scope">
                <!-- <el-input v-model="scope.row.userId" placeholder="请输入操作员" /> -->
              
                <el-select v-model="scope.row.userId"  placeholder="请选择操作员">
                  <el-option
                    v-for="item in userOptions"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>

              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" />
              </template>
            </el-table-column>
          </el-table>
         </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouses, getHouses, delHouses, addHouses, updateHouses,getOptionselect } from "@/api/btlfbreed/houses";

export default {
  name: "Houses",
  dicts: ['tb_chicken_houses_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbChickenDisinfectionRecords: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      isShow:true,
      userOptions: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 鸡舍信息表格数据
      housesList: [],
      // 消毒记录表格数据
      tbChickenDisinfectionRecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseName: null,
        houseCode: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        houseName: [
          { required: true, message: "鸡舍名称不能为空", trigger: "blur" }
        ],
        houseCode: [
          { required: true, message: "鸡舍编号不能为空", trigger: "blur" }
        ],
        capacity: [
          { required: true, message: "鸡舍容量不能为空", trigger: "blur" }
        ],
        currentCount: [
          { required: true, message: "当前存栏量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询鸡舍信息列表 */
    getList() {
      this.loading = true;
      listHouses(this.queryParams).then(response => {
        this.housesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    initData(){
      getOptionselect().then(response=>{
        this.userOptions=response.data;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        houseId: null,
        houseName: null,
        houseCode: null,
        capacity: null,
        status: null,
        currentCount: null,
        lastInspectionDate: null,
        userId: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.tbChickenDisinfectionRecordsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.houseId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.initData();
      this.reset();
      this.open = true;
      this.isShow=false;
      this.title = "添加鸡舍信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.initData();
      this.reset();
      this.isShow=true;
      const houseId = row.houseId || this.ids
      getHouses(houseId).then(response => {
        this.form = response.data;
        this.tbChickenDisinfectionRecordsList = response.data.tbChickenDisinfectionRecordsList;
        this.open = true;
        this.title = "修改鸡舍信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbChickenDisinfectionRecordsList = this.tbChickenDisinfectionRecordsList;
          if (this.form.houseId != null) {
            updateHouses(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouses(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const houseIds = row.houseId || this.ids;
      this.$modal.confirm('是否确认删除鸡舍信息编号为"' + houseIds + '"的数据项？').then(function() {
        return delHouses(houseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    	/** 消毒记录序号 */
    rowTbChickenDisinfectionRecordsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 消毒记录添加按钮操作 */
    handleAddTbChickenDisinfectionRecords() {
      let obj = {};
      obj.disinfectionDate = "";
      obj.disinfectantName = "";
      obj.disinfectantConcentration = "";
      obj.disinfectionMethod = "";
      obj.userId = "";
      obj.remark = "";
      this.tbChickenDisinfectionRecordsList.push(obj);
    },
    /** 消毒记录删除按钮操作 */
    handleDeleteTbChickenDisinfectionRecords() {
      if (this.checkedTbChickenDisinfectionRecords.length == 0) {
        this.$modal.msgError("请先选择要删除的消毒记录数据");
      } else {
        const tbChickenDisinfectionRecordsList = this.tbChickenDisinfectionRecordsList;
        const checkedTbChickenDisinfectionRecords = this.checkedTbChickenDisinfectionRecords;
        this.tbChickenDisinfectionRecordsList = tbChickenDisinfectionRecordsList.filter(function(item) {
          return checkedTbChickenDisinfectionRecords.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbChickenDisinfectionRecordsSelectionChange(selection) {
      this.checkedTbChickenDisinfectionRecords = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('btlfbreed/houses/export', {
        ...this.queryParams
      }, `houses_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
